var util = {};
util.login = function (userData) {
  var app = getApp();
  wx.login({
    success: res => {
      var code = res.code;
      var data = {
        code: code
      }
      app.request('wemini/login', data).then((succ) => {
        wx.setStorageSync('token', succ.data.token);
        var userInfo = {
          avatar: succ.data.member.avatar,
          mobile: succ.data.member.mobile,
          nickname: succ.data.member.nickname,
        }
        wx.setStorageSync('userInfo', userInfo);
        //用户信息不全
        if (!succ.data.member.has_get) {
          //用户授权  直接调用该接口(重新登录为该)
          wx.getUserInfo({
            lang: 'zh_CN',
            success: function (succ_info) {
              var userData = {
                iv: succ_info.iv,
                encryptedData: succ_info.encryptedData,
              };
              util.setUserInfo(userData);
            },
            fail: function (err_info) {
              //用户未进行授权  手动授权
              wx.reLaunch({
                url: '/pages/login/index',
              })
            }
          })
        } else {
          //用户信已有(即昵称,头像)   暂无手机号或者手机号错误     
          if (!(/^1(3|4|5|7|8)\d{9}$/.test(succ.data.member.mobile))) {
            wx.showModal({
              title: '温馨提示',
              content: '为了您的账户安全,以及提供更好的服务,请您绑定手机',
              showCancel: false,
              confirmText: '绑定手机',
              success: function (e) {
                if (e.confirm) {
                  wx.reLaunch({
                    url: '/pages/login/bind',
                  })
                }
              }
            })
          }
        }
      }).catch((err) => {
        wx.showModal({
          title: '温馨提示',
          content: '您的操作过于频繁',
          showCancel: false
        })
      })
    }
  })
}

util.setUserInfo = function (userData, callback) {
    var app = getApp();
    app.request('auth/getWxUserInfo', userData, true).then((succ) => {
      var userInfo = {
        avatar: succ.data.member.avatar,
        mobile: succ.data.member.mobile,
        nickname: succ.data.member.nickname,
      }
      wx.setStorageSync('userInfo', userInfo);
      if (!(/^1(3|4|5|7|8)\d{9}$/.test(succ.data.member.mobile))) {
        wx.showModal({
          title: '温馨提示',
          content: '为了您的账户安全,以及提供更好的服务,请您绑定手机',
          showCancel: false,
          confirmText: '绑定手机',
          success: function (e) {
            if (e.confirm) {
              wx.reLaunch({
                url: '/pages/login/bind',
              })
            }
          }
        })
      } else {
        wx.showToast({
          title: '登录成功',
          icon: 'success',
          duration: 600
        })
        if (callback && typeof (callback) === "function") callback();
      }
    }).catch((err) => {
      wx.showModal({
        title: '温馨提示',
        content: '您的操作过于频繁',
        showCancel: false
      })
    })
  },

  util.formid = function (formid) {
    var app = getApp();
    if ('the formId is a mock one' == formid) {
      console.log('请在手机端获取');
      return false;
    } else {
      var formData = {
        formid: formid
      };
      app.request('auth/collectFormid', formData, true).then((succ) => {
        console.log(succ);
      }).catch((err) => {
        console.log(err);
      })
    }
  }
module.exports = util;